using UnityEngine;
using System.Collections;

public class Builder : Player {
	
	public GameObject simpleBlock;
	public GameObject flyingEgg;
	public int blocksLeft = Balance.WINCOND_BLOCKS_START;
	
	private float layTime = 0.0f;
	private bool canLay = true;
	private ArrayList eggs;
	private bool eaten = false;
	private float slowDownTime;

    public AudioClip audioMultify;
    public AudioClip audioSlowDown;
    public AudioClip audioLayEgg;
	
	public Builder()
	{
		eggs = new ArrayList();
		movementSpeed = new AffectableSpeed(true);
		rotationSpeed = new AffectableSpeed(true);
		walkAnimationName = "Take 001";
		setItem(Item.Multify);//TODO remove
	}
	
	public override void Update()
	{
		//check for commands
		if(canLayEgg() && getCommandSource().consumePrimarySkill())
			layTime+= Time.deltaTime * (0.5f + BuildTimeManager.GetMultiplier(transform.position));
		else
		{
			layTime = 0;
			canLay = true;
		}
		if(layTime >= Balance.BUILDER_LAYTIME)
		{
			setBlock();
			canLay = false;
			layTime = 0;
		}
		handlePowerUps(Time.deltaTime);
		
		base.Update();
		
	}
	public override void reset()
	{
		layTime = 0.0f;
		canLay = true;
		eaten = false;
		foreach(GameObject egg in eggs)
			Destroy(egg);
		eggs.Clear();
	}
	public bool isEatable()
	{
		return eggs.Count == 0;
	}
	private bool canLayEgg()
	{
		if(blocksLeft <= 0)
			return false;
		
		bool distOk = true;
		foreach(GameObject egg in eggs)
		{
			if(egg != null && Vector3.Distance(egg.transform.position, transform.position) <= Balance.BUILDER_MINIMUM_EGG_DISTANCE)
			{
				Debug.Log("bad distance");
				distOk = false;
			}
		}
		
		return distOk && canLay;
		
	}
		
	private void setBlock()
	{
		blocksLeft--;
		eggs.Add(Instantiate(simpleBlock, transform.position, transform.rotation));
        audio.PlayOneShot(audioLayEgg);

	}
	
	public ArrayList getEggs()
	{
		return eggs;
	}
	
	public void removeEgg(GameObject egg)
	{
		eggs.Remove(egg);
	}
	public void setEaten(bool e)
	{
		eaten = e;
	}
	public bool isEaten()
	{
		return eaten;
	}
	
		//-----------------------------------Power Ups-----------------------------------------
	
	private void handlePowerUps(float deltaTime){
		slowDownTime = Mathf.Max(0, slowDownTime-deltaTime);

		if(slowDownTime == 0){
			Debug.Log("slowDown over");
			movementSpeed.disableBadEffect();
		}
	}
	
	public override void speedUp(){

	}
	
	public override void slowDown(){
		Debug.Log("slowDown activated");
		movementSpeed.enableBadEffect();
		slowDownTime = Balance.SLOW_DOWN_TIME;
        audio.PlayOneShot(audioSlowDown);
	}
	
	public void getSlowedDown(){
		Debug.Log("slowDown activated");
		movementSpeed.enableBadEffect();
		slowDownTime = Balance.SLOW_DOWN_TIME;
	}
	
	public override void multify()
	{
		for(int i = 0; i < 3; i++)
		{
			GameObject go = (GameObject) Instantiate(flyingEgg, gameObject.transform.position, gameObject.transform.rotation);
			eggs.Add(go);
			((FlyingEgg)(go.GetComponent<FlyingEgg>())).count = i;
		}
        audio.PlayOneShot(audioMultify);
	}
}
